import {Component, OnInit, Input} from '@angular/core';
import {ActivatedRoute, Params, Router} from '@angular/router';
import {HttpService} from '../../service/http.service';
import {Cookie} from 'ng2-cookies/ng2-cookies';
import {Email} from '../../module/email';
import {PushLog} from '../../module/pushLog';

@Component({
  selector: 'app-personal',
  templateUrl: './personal.component.html',
  styleUrls: ['./personal.component.css']
})
export class PersonalComponent implements OnInit {
  email: string;
  headimgurl: string;
  nickname: string;
  sex: string;
  vipDeadline: string;
  emails: Email[];
  pushLogs = [];
  endId = 0;
  newMailText: string;
  emailShowStatus = 'table';
  popupShow = false;
  deleteMailId: number;
  tab = 'mail';
  password = '';
  userId: number;
  userToken: string;
  tabValue = 'mail';
  loadMoreStatus = true;

  constructor(private route: ActivatedRoute,
              private httpService: HttpService) {
  }

  ngOnInit() {
    this.nickname = Cookie.get('nickname');
    this.headimgurl = Cookie.get('headimgurl');
    this.email = Cookie.get('email');
    this.sex = Cookie.get('sex');
    this.vipDeadline = Cookie.get('vipDeadline');
    this.getPushlogList();
    this.getMailList();
  }

  getMailList() {
    this.httpService.getEmails(Cookie.get('token')).then(t => this.emails = t);
  }

  getPushlogList() {
    this.httpService.getPushlog(Cookie.get('token'), this.endId).then(t => {
      if (t.code === 0 && t.data != null) {
        for (let i = 0; i < t.data.length; i++) {
          this.pushLogs.push(t.data[i]);
        }
        if (t.data.length > 0) {
          this.endId = t.data[t.data.length - 1].id;
        }
        if (t.data.length < 20) {
          this.loadMoreStatus = false;
        }
      } else {
        alert(t.msg);
      }
      console.log(this.endId);
    });
  }

  loadmoreLog() {
    this.getPushlogList();
  }

  postNewMail() {
    if (this.newMailText != null) {
      this.httpService.postEmail(Cookie.get('token'), this.newMailText).then(r => {
        if (r === 'success') {
          alert('保存成功!');
        } else {
          alert(r);
        }
        this.emailShowStatus = 'table';
        this.getMailList();
      });
    } else {
      alert('邮箱不能为空!');
    }
  }

  addMail() {
    this.emailShowStatus = 'add';
  }

  deleteMail(id: number) {
    console.log(id);
    this.deleteMailId = id;
    this.popupShow = true;
  }

  delete() {
    this.httpService.deleteEmail(Cookie.get('token'), this.deleteMailId).then(r => {
      if (r === 'success') {
        alert('删除成功!');
      } else {
        alert(r);
      }
      this.popupShow = false;
      this.getMailList();
    });
  }

  Popup(status: boolean) {
    this.popupShow = status;
  }

  showTab(t: string) {
    this.tabValue = t;
  }

  updateUser() {
    const token = Cookie.get('token');
    const userId = Cookie.get('id');
    console.log(userId);
    this.httpService.postModification(this.email, this.nickname, this.password, token, this.headimgurl, this.sex).then(r => {
      if (r.code === 0) {
        if (this.nickname === '') {
          alert('姓名不能为空');
        }else {
        console.log(r.data)
        this.nickname = r.data.nickname;
        Cookie.delete('email');
        Cookie.delete('token');
        Cookie.delete('id');
        Cookie.delete('nickname');
        Cookie.delete('sex');
        Cookie.delete('vipDeadline');
        Cookie.set('email', r.data.email);
        Cookie.set('headimgurl', r.data.headimgurl);
        Cookie.set('token', r.data.token);
        Cookie.set('id', '' + r.data.id);
        Cookie.set('nickname', r.data.nickname);
        Cookie.set('sex', r.data.sex + '');
        Cookie.set('vipDeadline', r.data.vipDeadline);
        alert('更新成功');
        }
      } else {
        alert(r.msg);
      }
      location.reload();
    });
  }

}
